// 引入axios组件
import axios from 'axios';
import qs from 'qs'; // 处理数据  qs.stringify(params)
import { checkRule } from '@/assets/js/public';
// 引入公共js文件
export default {
  data() {
    return {
      title: '', // 面包屑标题
      search: '',
      modal1: false, // 模态框显示开关，true显示   false不显示
      addbrand: false, // 模态框显示开关，true显示   false不显示
      param: '', // 点击编辑、删除的时候对应操作的那个id
      content: '', // 标签名称
      id: '', // 标签id
      putpage: '', // 存储页数
      columns7: [
        {
          title: 'id',
          width: 100,
          key: 'id',
          render: (h, params) => h('div', [h('strong', params.row.id)]),
        },
        {
          title: '内容',
          key: 'content',
          render: (h, params) => h('div', [h('strong', params.row.content)]),
        },
        {
          title: '操作时间',
          key: 'addtime',
          render: (h, params) => h('div', [h('strong', params.row.addtime)]),
        },
        {
          title: '操作选项',
          key: 'action',
          width: 150,
          align: 'center',
          render: (h, params) =>
            h('div', [
              h(
                'Button',
                {
                  props: {
                    type: 'info',
                    size: 'small',
                  },
                  style: {
                    marginRight: '5px',
                  },
                  on: {
                    click: () => {
                      this.show(params.row.id);
                      // this.show(params);
                    },
                  },
                },
                '编辑',
              ),
              h(
                'Button',
                {
                  props: {
                    type: 'error',
                    size: 'small',
                  },
                  style: {
                    marginRight: '5px',
                  },
                  on: {
                    click: () => {
                      this.remove(params);
                    },
                  },
                },
                '删除',
              ),
            ]),
        },
      ],
    };
  },
  created() {
    const self = this;
    this.$Spin.show();
    setTimeout(() => {
      this.$Spin.hide();
    }, 500);
    const spage = this.$route.query.page;
    const asearch = this.$route.query.search;
    axios({
      method: 'post',
      url: '/admin/gossip/gossipList',
      data: { page: spage, search: asearch },
    })
      .then((response) => {
        checkRule(response, self); // 检查是否有权限
        self.putpage = spage;
        self.$store.commit('getGossipParam', response.data);
      })
      .catch((response) => {
        console.log(response);
      });
  },
  // 用来获取VUEX中的文件
  computed: {
    data() {
      return this.$store.state.gossip.gossiplistshow;
    },
    page() {
      return this.$store.state.gossip.gossiplist.total;
    },
  },
  methods: {
    // 添加闲言碎语模态框
    addArticle() {
      this.param = '';
      this.content = '';
      this.addbrand = true;
    },
    // 执行添加修改闲言碎语
    ok_addbrand() {
      const self = this;
      axios({
        method: 'post',
        url: '/admin/gossip/gossipUpdate',
        data: { content: this.content, id: this.param },
      })
        .then((response) => {
          checkRule(response, self); // 检查是否有权限
          if (response.data.code == 1) {
            self.$Message.success(response.data.msg);
            self.$store.commit(
              'getGossipParamAgain',
              self.putpage,
              self.search,
            );
            self.data6.splice(param.index, 1); // 如果把那个1改成0，就是不删除
          } else {
            self.$Message.error(response.data.msg);
          }
        })
        .catch((response) => {
          // self.$Message.error('请求失败');
        }); //* /
    },
    // 点击取消之后走的方法
    cancel_addbrand() {
      this.addbrand = false;
    },
    // 分页的钩子函数，当跳页的时候重新请求（将页码发给后台）
    getpage(page) {
      console.log(page);
      if (page == undefined) {
        page = 1;
      }
      this.putpage = page;
      const self = this;
      // 分页
      axios({
        method: 'post',
        url: '/admin/gossip/gossipList',
        data: {
          page,
          search: self.search,
        },
      })
        .then((response) => {
          self.$store.commit('getGossipParam', response.data);
        })
        .catch((response) => {
          console.log('请求失败');
        });
      this.$router.push({
        path: '/gossip', // 这是路由名称
        query: { page, search: self.search },
      });
    },
    /**
     * 编辑当前品牌
     * localStorage html5新定义的属性，存在兼容问题  使用方法参考下边网址
     * https://www.cnblogs.com/st-leslie/p/5617130.html
     * https://segmentfault.com/q/1010000010175869?sort=created
     */
    show(index) {
      const self = this;
      axios({
        method: 'post',
        url: '/admin/gossip/gossipEdit',
        data: { goid: index },
      })
        .then((response) => {
          checkRule(response, self); // 检查是否有权限
          self.param = response.data.gossipinfo.id;
          self.content = response.data.gossipinfo.content;
        })
        .catch((response) => {
          self.$Message.error('请求失败');
        });
      this.addbrand = true;
    },
    /**
     * 删除当前标签
     * 同页面刷新不建议使用路由，再此请求数据
     * const定义的变量不可修改
     */
    remove(brandid) {
      this.modal1 = true;
      this.param = brandid;
    },
    ok() {
      const self = this; // 将this对象附到一个不可更改的变量中~
      axios({
        method: 'post',
        url: '/admin/gossip/gossipDel',
        data: { id: self.param.row.id },
      })
        .then((response) => {
          checkRule(response, self); // 检查是否有权限
          if (response.data.code == 1) {
            const numm = response.data.count;
            const mo = numm % 10; // 判断剩余总数摸10是否是0
            const pagepage = Math.ceil(numm / 10);
            if (
              self.param.index == 0 &&
              self.putpage != 1 &&
              mo == 0 &&
              self.putpage != pagepage
            ) {
              // 判断当前所在页是否是最后一页。
              self.putpage -= 1;
            }
            self.$store.commit(
              'getGossipParamAgain',
              self.putpage,
              self.search,
            );
            self.$Message.success(response.data.msg);
          } else {
            self.$Message.error(response.data.msg);
          }
        })
        .catch((response) => {});
    },
    cancel() {
      this.modal1 = false;
    },
  },
};
